﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuDada.Core.Post;
using WuDada.Core.Post.Service;
using WuDada.Core.Post.Domain;
using System.Text;
using WuDada.Core.Generic.Util;

public partial class case_in_bid : System.Web.UI.Page
{
    private PostFactory m_PostFactory;
    private IPostService m_PostService;
    private ITagService m_TagService;
    private ConfigHelper m_ConfigHelper = new ConfigHelper();
    private SessionHelper m_SessionHelper = new SessionHelper();
    private int m_PostId = 0;
    private int m_BidPrice = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        m_PostFactory = new PostFactory();
        m_PostService = m_PostFactory.GetPostService();
        m_TagService = m_PostFactory.GetTagService();
        m_PostId = QueryStringHelper.GetInteger("id", 0);
        m_BidPrice = QueryStringHelper.GetInteger("bid", 0);

        if (!IsPostBack)
        {
            hdnId.Value = m_PostId.ToString();
            hdnBidPrice.Value = m_BidPrice.ToString();
            FillVO();
        }
    }

    private void FillVO()
    {
        PostVO postVO = m_PostService.GetPostById(m_PostId);
        if (postVO != null)
        {
            UIHelper.FillUI(pnlUI, postVO);
            if (postVO.Price <= 0)
            {
                lblPrice.Text = string.Format("起標價：{0}", String.Format("{0:0,0}", postVO.StartPrice));
            }
            else
            {
                lblPrice.Text = string.Format("最高出價：{0}", String.Format("{0:0,0}", postVO.Price));
            }
            lblBidPrice.Text = m_BidPrice.ToString();
            imgPicFileName.ImageUrl = string.Format("upload/{0}", postVO.PicFileName);
        }
        else
        {
            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJSAndRedirect("查無此案件!", "index.aspx"), false);
        }
    }

    protected void aCancelBidding_Click(object sender, EventArgs e)
    {
        Response.Redirect(string.Format("case_in.aspx?id={0}", hdnId.Value));
    }

    protected void aBidding_Click(object sender, EventArgs e)
    {
        if (m_SessionHelper.Member != null)
        {            
            PostVO postVO = m_PostService.GetPostById(int.Parse(hdnId.Value));
            if (postVO != null)
            {
                if (m_SessionHelper.Member.MemberId.ToString().Equals(postVO.Winner))
                {
                    ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJSAndRedirect("您已是目前最高出價者!", string.Format("case_in.aspx?id={0}", hdnId.Value)), false);
                }
                else
                {
                    if (DateTime.Now >= postVO.ShowDate && DateTime.Now < postVO.CloseDate.Value.AddDays(1))
                    {
                        int bidPrice = int.Parse(hdnBidPrice.Value);
                        if (bidPrice >= postVO.StartPrice & bidPrice > postVO.Price)
                        {
                            TagVO tagVO = new TagVO()
                            {
                                OnwerId = m_SessionHelper.Member.MemberId.ToString(),
                                TargetId = hdnId.Value,
                                TagType = TagVO.Type.Bid,
                                Number = bidPrice,
                                CreatedDate = DateTime.Now,
                                CreatedBy = m_SessionHelper.Member.MemberId.ToString(),
                                UpdatedDate = DateTime.Now,
                                UpdatedBy = m_SessionHelper.Member.MemberId.ToString()
                            };
                            m_TagService.CreateTag(tagVO);

                            postVO.Price = bidPrice;
                            postVO.Winner = m_SessionHelper.Member.MemberId.ToString();
                            m_PostService.UpdatePost(postVO);

                            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJSAndRedirect("出價成功!", string.Format("case_in.aspx?id={0}", hdnId.Value)), false);
                        }
                        else
                        {
                            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJSAndRedirect("投標金額最少必須等於起標價或是高於目前出價!", string.Format("case_in.aspx?id={0}", hdnId.Value)), false);
                        }
                    }
                    else
                    {
                        ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJSAndRedirect("競標時間已過!", string.Format("case_in.aspx?id={0}", hdnId.Value)), false);
                    }
                }
            }            
        }
        else
        {
            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJS("請先登入會員!"), false);
        }
    }
}